Repository navigation
Rework install_unit() and remove_unit() - #449
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #449 +/- ##
==========================================
+ Coverage 92.95% 94.22% +1.26%
==========================================
Files 21 22 +1
Lines 1207 1315 +108
==========================================
+ Hits 1122 1239 +117
+ Misses 85 76 -9 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Enchufa2
force-pushed
the
rework-user-units
branch
from
November 13, 2026 11:26
de8e406 to
892b78b
Compare
Enchufa2
added a commit
that referenced
this pull request
Nov 13, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A refused install_unit() could leave symbols or names half mapped, and remove_unit() unmapped the symbol and name of whatever unit an id pointed to, so removing a leftover or an alias stripped an existing unit (#448). - install_unit() plans every mapping first and refuses before mapping anything when an id belongs to another unit; udunits errors are read from return codes and any applied mapping is rolled back on failure. - Symbols and names for a unit that already has one become aliases (e.g., install_unit("deg2", "1 degree")) instead of being refused. - id -> unit is mapped once (udunits ignores the encoding there), and unit -> id goes to the ASCII tree for ASCII ids, which every encoding falls back to (keeps #437 working). - A registry records the mappings each install made; remove_unit() undoes exactly those, promotes a remaining alias when a unit's own symbol or name is removed, and warns and leaves alone ids it did not install. - Ids are trimmed on a copy instead of in R's string cache; NA and empty ids are rejected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Pure move of the install/remove code out of udunits.cpp. The shared state (sys, enc) and registry_clear() are declared in a new udunits.h inside a `units` namespace, so they are not exported as bare globals. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
udunits' own UTF-8 check lets some invalid strings through (e.g. "\xff"), which were then stored as a unit's symbol. Check with validEnc() in install_unit() and remove_unit(), sharing the argument checks in .check_ids(). With conflicts refused when planning and invalid strings refused here, the rollback in ud_install() is only reachable on a system error, so mark it nocov. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Test argument checks, NA and empty ids, promotion of the oldest alias when there are several, and non-ASCII own symbols. Mark as nocov the two guards that cannot be reached: undoing a unit's own id that has changed since, and ud_install() without a unit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Enchufa2
force-pushed
the
rework-user-units
branch
from
November 13, 2026 12:18
892b78b to
ae73aa7
Compare
Enchufa2
added a commit
that referenced
this pull request
Nov 18, 2026
…database ids (#452) * Refuse ids that udunits already reads as a different unit install_unit() only checked the map of the same kind, but udunits reads an identifier by name (whatever its case), then by symbol, then the same after stripping a prefix. So a new name "m" or symbol "km" silently redefined a database unit, and a symbol "gal" was accepted but never reached, since the database name "gal" wins over it. - Refuse an id that udunits currently reads as a different unit. - Refuse a name with a case variant that is read as a different unit (e.g. "KG" would shadow "kg"). Names with at most 12 letters try every variant; no database symbol, prefixed or not, is longer than 8 characters, so longer names are checked against installed symbols only. - After mapping, every id must be read back as the new unit (e.g. "zz1" is read as zz^1); otherwise roll back and refuse. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Warn in remove_unit() about any id udunits reads as a unit remove_unit() only warned about ids it had not installed when they were in the map of their own kind, so removing "km" (read by prefix), a name "m" (a database symbol) or a symbol "gal" (a database name) did nothing silently. Decide by what udunits reads the id as instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Add override argument to install_unit() to redefine database ids Before #449, remove_unit() on a database id followed by install_unit() was the only way to give such an id another meaning, and it is no longer possible (#451). install_unit(..., override = TRUE) does it in one step and reversibly: - The database mappings that give an id another meaning are set aside: the id in both the symbol and name maps, and the unit's own symbol or name when it is that id. They are recorded under the installed id, and remove_unit() puts them back after undoing the install. - Setting aside, mapping and the read-back check run as one transaction, rolled back on any failure. - What each id meant before is reported with a message, by whatever route (e.g. "km" by prefix). - Ids installed by install_unit() must be removed first, and other ids a new name would shadow are still refused; case variants of the name itself (e.g. "microN" for "micron") are not. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * NEWS: merge the install_unit() entries, reference #452 and #451 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Share what an override set aside between both kinds of the same id A database mapping set aside by an override was filed under whichever installed id set it aside first. When an id was overridden as both a symbol and a name, removing the symbol first tried to put the database name back while the new name still held it; the failure went unnoticed, and the database name was lost once the new name was removed (#451). Any installed id, of either kind, that would set a mapping aside needs it out of the way: symbols as they are, names whatever their case. So when an id is removed, a mapping it set aside is handed to such an id if one is left, and put back only when none is. This works in any removal order, for ids installed in one call or in separate ones, and keeps the other kind's override working when only one kind is removed. - A mapping that cannot be put back is reported instead of ignored. - remove_unit() no longer warns "left unchanged" about an id that the same call put back, e.g. remove_unit("gal", "gal") after overriding only the symbol. - The override message and the docs say that overriding a symbol sets aside the name spelled the same in any letter case. Supersedes #453, whose test is included. Co-Authored-By: Robin Shannon <32329438+vorpalvorpal@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Robin Shannon <32329438+vorpalvorpal@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #448. Supersedes #447.
Problem
install_unit()maps each symbol/name with several separate udunits calls (id → unit and unit → id, in more than one encoding), so a refusal halfway leaves some of them behind.remove_unit()then unmaps the symbol and name of whatever unit an id points to, so removing a leftover, or any alias, strips an existing unit:Reordering the calls doesn't help, because udunits keeps a unit's own symbol in per-encoding trees with lookup fallbacks: degree's symbol is in the UTF-8 tree, m's is in the ASCII tree, so every fixed order corrupts some unit (e.g., UTF-8 first turns
1 [m]into1 [zzm]).Changes
install_unit("deg2", "1 degree")now works and degree keeps°for output.convert_to_base()to user-defined base units? #437 keeps working.remove_unit()undoes exactly those, hands the unit's own symbol/name to a remaining alias when it is removed, and leaves ids it didn't install untouched with a warning. The registry is cleared with the unit system inload_units_xml().src/user_units.cpp(separate commit, pure move).Behavior change:
remove_unit()on a symbol/name from the database (e.g.remove_unit("m")) used to remove it; now it warns and does nothing. Removing an unknown id is still silent.Testing
test_user_conversion.R: refused installs map nothing, aliases, alias removal and promotion, removal of database units, ASCII availability. They fail onmain(7 errors/failures) and pass here; the full suite passes.main: 3 units corrupted under UTF-8).🤖 Generated with Claude Code